package com.stonedt.api.test;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
 * 获取token工具类
 * @author wangyi
 *
 */
public class TokenUtil {
	public static void main(String[] args) throws ParseException, IOException, InterruptedException {
		//1.1 添加关注微信公众号“思通数据”。
		//1.2 点击左侧第一个菜单，“数据工具"-"获取授权”，将会自动返回appId和appSecret。
		//1.3 将获取的授权码填入测试程序TokenUtil的第20-21行中进行模拟测试。
		//1.4 当看见返回结果“200”表示授权,并已成功获取token值。
	    String appId = "**************";//appId
	    String appSecret = "*************";//appSecret
		String gettoken = gettoken("http://data.stonedt.com/auth/token?appid="+appId+"&appsecret="+appSecret);
		System.out.println(gettoken);
	}
	
	
	public static String gettoken(String url) throws ParseException, IOException, InterruptedException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String string = null;
		HttpGet httpget = new HttpGet(url);
		RequestConfig config = RequestConfig.custom().setConnectTimeout(10 * 1000).setSocketTimeout(20 * 1000).build();
		httpget.setConfig(config);
		CloseableHttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			string = EntityUtils.toString(entity, "gbk");
		}
		response.close();
		httpclient.close();
		return string;
	}

}
